﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using MySql.Data.MySqlClient;
using CodeRobot.Utility;
using System.Windows.Forms;

namespace CodeRobot.DAL
{
    /// <summary>
    /// .net core处理请求类
    /// </summary>
    public class ViewMutiCreateHelper
    {

        /// <summary>
        /// 根据表名创建Create新增页面
        /// </summary>
        /// <param name="strFilePath">保存路径</param>
        /// <param name="strProjectName">项目名</param>
        /// <param name="strTableName">表名</param>
        public static void CreateViewMutiCreateClass(string strFilePath,string strProjectName, string strTableName,string strTableComment, string strDBName)
        {
            //读取版权信息
            CodeRobot.Utility.IniFile iniFile = new Utility.IniFile(Application.StartupPath + "\\config.ini");
            string strCompany = iniFile.GetString("COPYRIGHT", "COMPANY", "");
            string strAuthor = iniFile.GetString("COPYRIGHT", "AUTHOR", "");
            string strVersion = iniFile.GetString("COPYRIGHT", "VERSION", "");
            string strCode = iniFile.GetString("COPYRIGHT", "CODE", "");
            string strCreateDate = iniFile.GetString("BASE", "CREATE_DATE", "");
            string strTablePrefix = iniFile.GetString("BASE", "TABLE_PREFIX", "");
            string strTableNameUpper = CommonHelper.GetTableNameUpper(strTableName);
            
            string strPrimaryKey = CommonHelper.GetPrimaryKey(strTableName);//如：news_id

            string strAllColumnName = CommonHelper.GetAllColumnName(strTableName);
            string strAllColumnNameNotKey = CommonHelper.GetAllColumnNameNotKey(strTableName);

            string strClassName = CommonHelper.GetClassName(strTableName);//类名
            strClassName = CommonHelper.GetTableNameUpper(strClassName);
            string strTableNameSpec = CommonHelper.GetTableNameFirstLowerSecondUpper(strClassName);//如：newsTypes
            string strTableNameLower = strTableNameSpec.ToLower();//如：newstypes

            Directory.CreateDirectory(strFilePath);
            //为每个表创建一个独立目录存放
            string strTableNamePath = strFilePath + "\\"+ strClassName;
            if (!Directory.Exists(strTableNamePath))
            {
                Directory.CreateDirectory(strTableNamePath);
            }
            StreamWriter sw = new StreamWriter(strTableNamePath + "\\Create.cshtml", false, Encoding.GetEncoding("utf-8"));//生成物理文件
            string strTableComment2 = CommonHelper.GetColumnKeyComment(strTableComment);
            string strRichEdit = CommonHelper.GetRichEditColumnName(strDBName, strTableName);

            sw.WriteLine("@{");
            sw.WriteLine("    ViewData[\"Title\"] = \"新增"+ strTableComment2 + "\";");
            sw.WriteLine("    Layout = \"~/Views/Shared/_LayoutNone.cshtml\";");
            sw.WriteLine("}");
            sw.WriteLine("<div class=\"layui-fluid\">");
            sw.WriteLine("    <div class=\"layui-row layui-col-space15\">");
            sw.WriteLine("        <div class=\"layui-col-md12\">");
            sw.WriteLine("            <div class=\"layui-card\">");
            sw.WriteLine("                <div class=\"layui-card-body\" pad15>");
            sw.WriteLine("                    <div class=\"layui-form\" lay-filter=\"layuiadmin-app-form-list\" id=\"layuiadmin-app-form-list\" style=\"padding: 20px 30px 0 0;\">");
            sw.WriteLine(CommonHelper.GetCreateItemList(strDBName,strTableName));
            sw.WriteLine("                        <div class=\"layui-form-item\">");
            sw.WriteLine("                            <label class=\"layui-form-label\"></label>");
            sw.WriteLine("                            <div class=\"layui-input-inline\">");
            sw.WriteLine(CommonHelper.GetUploadImgHiddenInput(strDBName, strTableName));
            // sw.WriteLine("            <input type=\"hidden\" id=\"created_at\" name=\"created_at\" value=\"@DateTime.Now.ToString()\" />");
            sw.WriteLine("                                <input type=\"button\" class=\"layui-btn\" lay-submit lay-filter=\"layuiadmin-app-form-edit\" id=\"layuiadmin-app-form-edit\" value=\"确认提交\">");
            sw.WriteLine("                            </div>");
            sw.WriteLine("                        </div>");
            sw.WriteLine("                    </div>");
            sw.WriteLine("                </div>");
            sw.WriteLine("            </div>");
            sw.WriteLine("        </div>");
            sw.WriteLine("    </div>");
            sw.WriteLine("</div>");
            sw.WriteLine("");
            sw.WriteLine("<script src=\"~/layuiadmin/layui/layui.js\"></script>");
            sw.WriteLine("<script>");
            sw.WriteLine("    layui.config({");
            sw.WriteLine("        base: '/layuiadmin/' // 静态资源所在路径");
            sw.WriteLine("    }).extend({");
            sw.WriteLine("        index: '/lib/index' // 主入口模块");
            sw.WriteLine("    }).use(['index', 'layedit', 'form', 'upload', 'laydate'], function () {");
            sw.WriteLine("        var $ = layui.$");
            sw.WriteLine("            , form = layui.form");
            sw.WriteLine("            , admin = layui.admin");
            sw.WriteLine("            , upload = layui.upload");
            sw.WriteLine("            , layedit = layui.layedit");
            sw.WriteLine("            , laydate = layui.laydate;");
            sw.WriteLine("");
            //sw.WriteLine("        //laydate.render({");
            //sw.WriteLine("            //elem: '#此处字段需要根据实际业务逻辑修改'");
            //sw.WriteLine("        //});");
            if (!string.IsNullOrEmpty(strRichEdit))
            {
                sw.WriteLine("        var index = layedit.build('edit_content', {");
                sw.WriteLine("            height: 300");
                sw.WriteLine("            , uploadImage: {");
                sw.WriteLine("                multiple: true,");
                sw.WriteLine("                url: \"/upload/uploadimg?catalogue=service\"");
                sw.WriteLine("                , type: 'post'");
                sw.WriteLine("                , headers: {");
                sw.WriteLine("                    'RequestVerificationToken': csrfToken");
                sw.WriteLine("                }");
                sw.WriteLine("            }");
                sw.WriteLine("        }); //建立编辑器");
            }
            sw.WriteLine("");
            if (CommonHelper.GetSelectScript(strProjectName, strDBName, strTableName, strTablePrefix).Contains("admin.req") || CommonHelper.GetSelectArea(strProjectName, strDBName, strTableName).Contains("admin.req"))
            {
                sw.WriteLine("        // 默认加载");
                sw.WriteLine("        $(function () {");
                // sw.WriteLine("            //读取数据加载loading..");
                sw.WriteLine("            loading = layer.load(2, {");
                sw.WriteLine("                shade: [0.2, '#000']");
                sw.WriteLine("            });");
                sw.WriteLine(CommonHelper.GetSelectScript(strProjectName, strDBName, strTableName, strTablePrefix));
                sw.WriteLine(CommonHelper.GetSelectArea(strProjectName, strDBName, strTableName));
                sw.WriteLine("        });");
                sw.WriteLine("");
            }
            sw.WriteLine("");
            sw.WriteLine(CommonHelper.GetSwitchScript(strDBName, strTableName));
            sw.WriteLine("");
            sw.WriteLine(CommonHelper.GetUploadImgScript(strProjectName, strDBName, strTableName));
            sw.WriteLine("");
            sw.WriteLine(CommonHelper.GetAreaScriptListen(strProjectName, strDBName, strTableName));
            sw.WriteLine("");
            sw.WriteLine("        // 监听提交");
            sw.WriteLine("        form.on('submit(layuiadmin-app-form-edit)', function (data) {");
            if (!string.IsNullOrEmpty(strRichEdit))
            {
                sw.WriteLine("            data.field." + CommonHelper.GetEditorKey(strDBName, strTableName) + " = layedit.getContent(index);");
            }
            sw.WriteLine("            loading = layer.load(2, {");
            sw.WriteLine("                shade: [0.2, '#000']");
            sw.WriteLine("            });");
            sw.WriteLine("");
            sw.WriteLine("            // 提交数据");
            sw.WriteLine("            admin.req({");
            sw.WriteLine("                headers: {");
            sw.WriteLine("                    'RequestVerificationToken': csrfToken");
            sw.WriteLine("                },");
            sw.WriteLine("                method: 'POST',");
            sw.WriteLine("                url: '/"+ strTableNameLower + "/create'");
            sw.WriteLine("                , data: data.field");
            sw.WriteLine("                , done: function (res) {");
            sw.WriteLine("                    // console.log(`res=${JSON.stringify(res)}`)");
            sw.WriteLine("                    layer.close(loading);");
            sw.WriteLine("                    if(res.errcode){");
            sw.WriteLine("                        layer.open({");
            sw.WriteLine("                          title: '错误提示'");
            sw.WriteLine("                          ,type: 1");
            sw.WriteLine("                           ,content: '<div style=\"color: red; padding: 20px; \">'+ res.errcode +':'+ res.errmsg +'</div>'");
            sw.WriteLine("                          ,btn: [\"确定\"]");
            sw.WriteLine("                        });");
            sw.WriteLine("                    } else {");
            sw.WriteLine("                        layer.msg(res.msg, {");
            sw.WriteLine("                            offset: '15px'");
            sw.WriteLine("                            , icon: 1");
            sw.WriteLine("                            , time: 2000");
            sw.WriteLine("                        }, function () {");
            sw.WriteLine("                            window.location.reload();");
            sw.WriteLine("                        });");
            sw.WriteLine("                    }");
            sw.WriteLine("                }");
            sw.WriteLine("            });");
            sw.WriteLine("        });");
            sw.WriteLine("    })");
            sw.WriteLine("</script>");
            sw.Close();


        }
    }
}
